<template>
  <view class="uni-container">
    <uni-forms ref="form" :model="formData" validateTrigger="bind">
      <uni-forms-item name="merchant_id" label="所属商户">
        <uni-data-picker v-model="formData.merchant_id" collection="food-merchant-list" field="store_name as text, _id as value"></uni-data-picker>
      </uni-forms-item>
      <uni-forms-item name="user_id" label="所属用户">
        <uni-data-picker v-model="formData.user_id" collection="uni-id-users" field="nickname as text, _id as value"></uni-data-picker>
      </uni-forms-item>
      <uni-forms-item name="wallet_type" label="钱包类型">
        <uni-data-checkbox v-model="formData.wallet_type" :localdata="formOptions.wallet_type_localdata"></uni-data-checkbox>
      </uni-forms-item>
      <uni-forms-item name="trade_type" label="交易类型">
        <uni-data-checkbox v-model="formData.trade_type" :localdata="formOptions.trade_type_localdata"></uni-data-checkbox>
      </uni-forms-item>
      <uni-forms-item name="trade_id" label="流水单号">
        <uni-easyinput placeholder="由那个模块生成的" v-model="formData.trade_id"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="change_before_amount" label="变动前金额">
        <uni-easyinput placeholder="变动前钱包所持有的金额，必须是分为单位" type="number" v-model="formData.change_before_amount"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="changet_after_amount" label="变动后金额">
        <uni-easyinput placeholder="变动后钱包所持有的金额，必须是分为单位" type="number" v-model="formData.changet_after_amount"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="commission_percentage" label="平台抽成">
        <uni-easyinput placeholder="生成该记录时店铺的抽成千分比" type="number" v-model="formData.commission_percentage"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="change_amount" label="变动金额">
        <uni-easyinput placeholder="变动金额，必须是分为单位" type="number" v-model="formData.change_amount"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="real_get_amount" label="实际获得">
        <uni-easyinput placeholder="实际获得金额，必须是分为单位" type="number" v-model="formData.real_get_amount"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="commission_amount" label="抽成金额">
        <uni-easyinput placeholder="抽成金额，必须是分为单位" type="number" v-model="formData.commission_amount"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="remark" label="备注">
        <uni-easyinput type="textarea" placeholder="备注" v-model="formData.remark" trim="both"></uni-easyinput>
      </uni-forms-item>
      <view class="uni-button-group">
        <button type="primary" class="uni-button" style="width: 100px;" @click="submit">提交</button>
        <navigator open-type="navigateBack" style="margin-left: 15px;">
          <button class="uni-button" style="width: 100px;">返回</button>
        </navigator>
      </view>
    </uni-forms>
  </view>
</template>

<script>
  import { validator } from '../../js_sdk/validator/food-merchant-bill.js';

  const db = uniCloud.database();
  const dbCmd = db.command;
  const dbCollectionName = 'food-merchant-bill';

  function getValidator(fields) {
    let result = {}
    for (let key in validator) {
      if (fields.includes(key)) {
        result[key] = validator[key]
      }
    }
    return result
  }

  

  export default {
    data() {
      let formData = {
        "merchant_id": "",
        "user_id": "",
        "wallet_type": 0,
        "trade_type": 0,
        "trade_id": "",
        "change_before_amount": null,
        "changet_after_amount": null,
        "commission_percentage": null,
        "change_amount": null,
        "real_get_amount": null,
        "commission_amount": null,
        "remark": ""
      }
      return {
        formData,
        formOptions: {
          "wallet_type_localdata": [
            {
              "value": 0,
              "text": "冻结金额"
            },
            {
              "value": 1,
              "text": "可用金额"
            }
          ],
          "trade_type_localdata": [
            {
              "value": 0,
              "text": "收入"
            },
            {
              "value": 1,
              "text": "提现"
            },
            {
              "value": 2,
              "text": "扣款"
            },
            {
              "value": 3,
              "text": "充值"
            },
            {
              "value": 4,
              "text": "划转"
            }
          ]
        },
        rules: {
          ...getValidator(Object.keys(formData))
        }
      }
    },
    onReady() {
      this.$refs.form.setRules(this.rules)
    },
    methods: {
      
      /**
       * 验证表单并提交
       */
      submit() {
        uni.showLoading({
          mask: true
        })
        this.$refs.form.validate().then((res) => {
          return this.submitForm(res)
        }).catch(() => {
        }).finally(() => {
          uni.hideLoading()
        })
      },

      /**
       * 提交表单
       */
      submitForm(value) {
        // 使用 clientDB 提交数据
        return db.collection(dbCollectionName).add(value).then((res) => {
          uni.showToast({
            title: '新增成功'
          })
          this.getOpenerEventChannel().emit('refreshData')
          setTimeout(() => uni.navigateBack(), 500)
        }).catch((err) => {
          uni.showModal({
            content: err.message || '请求服务失败',
            showCancel: false
          })
        })
      }
    }
  }
</script>
